<?php
    $path = $_SERVER['DOCUMENT_ROOT'];
   $db_path = $path."/lib/database.php";
   include_once($db_path);
      
class _player{

	private $pid;
	private $login;
	private $pass;
	private $email;
	private $desc;
	private $name;
	private $type;
	private $active;
	
	
	/* The limit used to set a number of days  where inactivity changes status */
	private static $_ACTIVE_LIMIT = 14;


	public function logout(){
		$this->pid=0;
		$this->login='';
		$this->pass='';
		$this->email='';
		$this->desc='';
		$this->name='';
		$this->type='';
		$this->active='';
	}
	
	
	
	/*  This function is used to load the player into a player array based on pid.  Not to be used to login as it will not keep password info / update when last logged in.*/
	public function load($pid){
		$q = "select * from PLAYERS where login_id = '".$login_sha."'";
		$r = mysql_query($q);
		if($r !== false && mysql_num_rows($r)){
			if($a = mysql_fetch_assoc($r)){
				$this->pid   = stripslashes($a['PLAYER_ID']);
				$this->login = stripslashes($a['LOGIN']);
				$this->email = stripslashes($a['EMAIL']);
				$this->name  = stripslashes($a['NAME']);
				$this->desc  = stripslashes($a['ABOUT']);
				$this->type  = stripslashes($a['TYPE']);
				$this->active = stripslashes($a['ACTIVE']);
				return true;
			}		
		}
		return false;	
	}
	
	public function login($login, $pass){
		dblogin();
		$login = strtolower($login);
		$login_sha = sha1($login);

		$q = "select * from PLAYERS where login_id = '".$login_sha."'";
		$r = mysql_query($q);
		if($r !== false && mysql_num_rows($r)){
			if($a = mysql_fetch_assoc($r)){
				if($a['PASS'] == sha1($pass)){
					$this->pid   = stripslashes($a['PLAYER_ID']);
					$this->login = stripslashes($a['LOGIN']);
					$this->pass  = stripslashes($a['PASS']);
					$this->email = stripslashes($a['EMAIL']);
					$this->name  = stripslashes($a['NAME']);
					$this->desc  = stripslashes($a['ABOUT']);
					$this->type  = stripslashes($a['TYPE']);
					$this->active = stripslashes($a['ACTIVE']);
					
					$this->update_login_dt();
				}else{
					echo "Login and/or password incorrect<br>";
					//array_push($error, "Login and/or password incorrect");
				}
			}		
		}else{
			echo "Login and/or password incorrect<br>";
			//array_push($error, "Login and/or password incorrect");
		}
	}

	private function update_login_dt(){
		//Update the last login date to NOW
		$q = "update PLAYERS set lst_login_dt = NOW() where player_id = "$this->pid;
		$r = mysql_query($q);			
	}
	public function update_lst_activity_dt(){
		//Update the last activity to NOW
		$q = "update PLAYERS set lst_activity_dt = NOW() where player_id = "$this->pid;
		$r = mysql_query($q);
	}

	public function is_active(){
		return $this->active;
	}
	
	static function update_status(){
		$q = "select pid, lst_login_dt, lst_activity_dt from PLAYERS";
		$r = mysql_query($q);
		if($r !== false && mysql_num_rows($r)){
			while($a = mysql_fetch_assoc($r)){
				if(_player::$OPTION_LOGIN){//Use Login Date as criteria for active/inactive
					if( strtotime("now") - $a['lst_login_dt'] > _player::$_ACTIVE_LIMIT){//set player to inactive
						$inactq = "update PLAYERS set active = 'N' where player_id = "$a['pid'];
						$inactr = mysql_query($inactq);														
					}else{//set player to active
						$actq = "update PLAYERS set active = 'Y' where player_id = "$a['pid'];
						$actr = mysql_query($actq);
					}
				}
				
				if(_player::$OPTION_ACTIVITY){//Use last activity as criteria for active/inactive
					if(strtotime("now") - $a['lst_activity_dt'] > _player::$_ACTIVE_LIMIT){//set player to inactive
						$inactq = "update PLAYERS set active = 'N' where player_id = "$a['pid'];
						$inactr = mysql_query($inactq);										
					}else{//set player to active
						$actq = "update PLAYERS set active = 'Y' where player_id = "$a['pid'];
						$actr = mysql_query($actq);					
					}
				}	
			}
		}	
	}
	
	public function is_loggedin(){
		return ($this->pid != 0);	
	}
	public function is_admin(){
		if($this->type == 'A'){
			return true;
		}
		return false;
	}
	public function get_nbr(){
		return $this->pid;
	}
	public function name(){
		return $this->name;
	}
	
	public function forgot_pw(){
		//Generate a random password
		$rand_array = array('a','b','c','d','e','f','g','h','i','j','1','2','3','4','5','6','7','8','9');

		$tmp_pass='';
		for($i=10; $i > 0; $i--){
			$t = rand(1,18);
			$tmp_pass .= $rand_array[$t];
		}
		
		//email out the new password
		$this->email_temp_password($tmp_pass);

		$q = "update PLAYERS set pass = '".sha1($tmp_pass)."' where player_id = ".$this->pid;
		echo $q,'<br>';
		if($r = mysql_query($q)){
			echo 'Password updated correctly<br>';		
		}else{
			echo 'Error updating password<br>';
			//array_push($error,"PASSWORD update error");
		}	
	}

	private function email_temp_password($temp_pass){
		mail($this->email, 'Your temporary FNT password', 'Here is your temporary FNT password: '.$temp_pass);
	}
	
	public function form_login(){
$output = '
		<form action="'.$_SERVER['PHP_SELF'].'" method="post">
			<label for="login">Login:</label>
			<input name="login" id="login" type="text" maxlength="50" />
			<label for="password">Password:</label>
			<input name="password" id="password" type="password" />
			<input type="submit" name="submit" value="Login" /></form>';		
		return $output;	
	}

	public function form_logout(){
		$output = '
		<form action="'.$_SERVER['PHP_SELF'].'" method="post">
			<input type="submit" name="submit" value="Logout" /></form>';		
		return $output;	
	}

	public function pw_chg ($oldpw, $newpw1, $newpw2){
		dblogin();
		$oldpw = sha1($oldpw);
		$newpw1 = sha1($newpw1);
		$newpw2 = sha1($newpw2);
		if($oldpw == $this->pass){
			if($newpw1 == $newpw2 && $newpw1){
				//set The password
				$q = "update PLAYERS p set PASS = '".$newpw1."' where player_id = ".$this->pid;
				if($r = mysql_query($q)){
					echo 'Password updated<br>';
				}else{
					echo 'Please check new passwords<br>';
					//array_push($error, "Please check new passwords");
				}
			}
		}else{
			echo "Old password doesn't match<br>";
			//array_push($error, "Old password doesn't match");
		}
	}
}

?>
